<?php
/**
 * TomatoCMS
 * 
 * LICENSE
 *
 * This source file is subject to the GNU GENERAL PUBLIC LICENSE Version 2 
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.gnu.org/licenses/gpl-2.0.txt
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@tomatocms.com so we can send you a copy immediately.
 * 
 * @copyright	Copyright (c) 2009-2010 TIG Corporation (http://www.tig.vn)
 * @license		http://www.gnu.org/licenses/gpl-2.0.txt GNU GENERAL PUBLIC LICENSE Version 2
 * @version 	$Id: ShopController.php 4929 2010-08-25 03:36:50Z jam-gate.vn $
 * @since		2.0.0
 */

class Shop_ShopController extends Zend_Controller_Action 
{
	/* ========== Backend actions =========================================== */

	/**
	 * Add new shop
	 * 
	 * @return void
	 */
	public function addAction() 
	{
		$conn = Tomato_Db_Connection::factory()->getMasterConnection();
		$shopDao = Tomato_Model_Dao_Factory::getInstance()->setModule('shop')->getShopDao();
		$shopDao->setDbConnection($conn);
		$nextId 	= 	$shopDao->getNextId();
		$this->view->assign("nextId",$nextId);
		
		$request  = $this->getRequest();
		
		$this->view->assign('lang', $request->getParam('lang'));
		
		if ($request->isPost()) {
			$user 	  = Zend_Auth::getInstance()->getIdentity();
			
			$shop = new Shop_Models_Shop(array(
			));
			$id = $shopDao->add($shop);
			
			$this->_helper->getHelper('FlashMessenger')
				->addMessage($this->view->translator('shop_add_success'));
			$this->_redirect($this->view->serverUrl() . $this->view->url(array(), 'shop_shop_add'));
		}
	}
	
	/**
	 * Delete shop
	 * 
	 * @return void
	 */
	public function deleteAction() 
	{
		$this->_helper->getHelper('layout')->disableLayout();
		$this->_helper->getHelper('viewRenderer')->setNoRender();
		
		$response = 'RESULT_ERROR';
		$request  = $this->getRequest();
		if ($request->isPost()) {
			$id = $request->getPost('id');
			$conn = Tomato_Db_Connection::factory()->getMasterConnection();
			$shopDao = Tomato_Model_Dao_Factory::getInstance()->setModule('shop')->getShopDao();
			$shopDao->setDbConnection($conn);
			$shop = $shopDao->getById($id);
			
			if ($shop != null) {
				$shopDao->delete($shop);
				$response = 'RESULT_OK';
			}
		}
		$this->getResponse()->setBody($response);
	}
	
	/**
	 * Edit shop
	 * 
	 * @return void
	 */
	public function editAction() 
	{		  
		$request = $this->getRequest();
		$conn = Tomato_Db_Connection::factory()->getMasterConnection();
		$shopDao = Tomato_Model_Dao_Factory::getInstance()->setModule('shop')->getShopDao();
		$shopDao->setDbConnection($conn);
		
		$shopId = $request->getParam('shop_id');
		$shop 	= $shopDao->getById($shopId);
		if (null == $shop) {
			throw new Exception('Not found shop with id of ' . $shopId);
		}
		$this->view->assign('shop', $shop);		 		
		if ($request->isPost()) {		
			//$shopDao->update($shop);
			$this->_helper->getHelper('FlashMessenger')
				->addMessage($this->view->translator('shop_edit_success'));			
			$this->_redirect($this->view->serverUrl() . $this->view->url(array('shop_id' => $shopId), 'shop_shop_edit'));			
		}		
	}
	
	/**
	 * List categories
	 * 
	 * @return void
	 */
	public function listAction() 
	{
		$conn = Tomato_Db_Connection::factory()->getMasterConnection();
		$shopDao = Tomato_Model_Dao_Factory::getInstance()->setModule('shop')->getShopDao();
		$shopDao->setDbConnection($conn);
		
		$lang = $this->getRequest()->getParam('lang', Tomato_Config::getConfig()->web->lang);
		
		$shopDao->setLang($lang);
        $request   = $this->getRequest();
		$pageIndex = $request->getParam('pageIndex', 1);
        $perPage   = Tomato_Config::getConfig()->manager->itemPerPage;;
        $offset    = ($pageIndex - 1) * $perPage;
        $exp = array(
        	   );
		$shops 	 = $shopDao->find($offset, $perPage, $exp);
		$this->view->assign('shops', $shops);
		
		$total       = $shopDao->count($exp);        
		$paginator = new Zend_Paginator(new Tomato_Utility_PaginatorAdapter($shops, $total));
        $paginator->setCurrentPageNumber($pageIndex);
        $paginator->setItemCountPerPage($perPage);
        
		$this->view->assign('paginator', $paginator);
        $this->view->assign('paginatorOptions', array(
            'path'        => $this->view->url(array(), 'shop_shop_list'),
            'itemLink' 	  => 'page-%d'                          
        ));
        $this->view->assign('lang',$lang);				
	} 		
}	